What is php-parser?
The php-parser npm package is a JavaScript library that allows you to parse PHP code into an Abstract Syntax Tree (AST). This can be useful for various tasks such as static analysis, code transformation, and code generation.
What are php-parser's main functionalities?
Parsing PHP Code
This feature allows you to parse PHP code into an Abstract Syntax Tree (AST). The code sample demonstrates how to parse a simple PHP script and output the resulting AST.
const parser = require('php-parser');
const phpParser = new parser({ parser: { extractDoc: true } });
const ast = phpParser.parseCode('<?php echo "Hello, World!"; ?>');
console.log(JSON.stringify(ast, null, 2));
Traversing the AST
This feature allows you to traverse the AST to find specific nodes. The code sample demonstrates how to traverse the AST to find and log echo statements.
const parser = require('php-parser');
const phpParser = new parser({ parser: { extractDoc: true } });
const ast = phpParser.parseCode('<?php echo "Hello, World!"; ?>');
function traverse(node) {
if (node.kind === 'echo') {
console.log('Found an echo statement');
}
for (let key in node) {
if (node[key] && typeof node[key] === 'object') {
traverse(node[key]);
}
}
}
traverse(ast);
Modifying the AST
This feature allows you to modify the AST. The code sample demonstrates how to change the output of an echo statement from 'Hello, World!' to 'Hello, Universe!'.
const parser = require('php-parser');
const phpParser = new parser({ parser: { extractDoc: true } });
let ast = phpParser.parseCode('<?php echo "Hello, World!"; ?>');
function modifyEcho(node) {
if (node.kind === 'echo') {
node.arguments[0].value = 'Hello, Universe!';
}
for (let key in node) {
if (node[key] && typeof node[key] === 'object') {
modifyEcho(node[key]);
}
}
}
modifyEcho(ast);
console.log(JSON.stringify(ast, null, 2));
Other packages similar to php-parser
esprima
Esprima is a high-performance, standard-compliant ECMAScript parser written in JavaScript. It is used for parsing JavaScript code into an AST. While php-parser is used for PHP, Esprima serves a similar purpose for JavaScript.
acorn
Acorn is a small, fast, JavaScript-based JavaScript parser. It generates an AST from JavaScript code and is known for its performance and modularity. Like php-parser, it is used for parsing code into an AST, but it is specific to JavaScript.
php-parser
This JavaScript library parses PHP code and converts it to an AST.
Installation
This library is distributed with npm :
npm install php-parser --save
Usage
const fs = require("fs");
const path = require("path");
const engine = require("php-parser");
const parser = new engine({
parser: {
extractDoc: true,
php7: true,
},
ast: {
withPositions: true,
},
});
const eval = parser.parseEval('echo "Hello World";');
const tokens = parser.tokenGetAll('<?php echo "Hello World";');
const phpFile = fs.readFileSync("./example.php");
console.log("Eval parse:", eval);
console.log("Tokens parse:", tokens);
console.log("File parse:", parser.parseCode(phpFile));
Sample AST output
{
'kind': 'program',
'children': [
{
'kind': 'echo',
'arguments': [
{
'kind': 'string',
'isDoubleQuote': true,
'value': 'Hello World'
}
]
}
]
}
API Overview
The main API exposes a class with the following methods :
- parseEval(String|Buffer) : parse a PHP code in eval style mode (without php open tags)
- parseCode(String|Buffer, String filename) : parse a PHP code by using php open tags.
- tokenGetAll(String|Buffer) : retrieves a list of all tokens from the specified input.
You can also pass options that change the behavior of the parser/lexer.
Documentation
Related projects
You can add here your own project by opening an issue request.
License
This library is released under BSD-3 license clause.